﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace S2_SmartCode
{
    public class Model
    {
        public static void Create(string strModelNameSpace, string strModel)
        {
            string str = strModel;
            DataTable table = Common.LoadTableColumns(str);
            if (table != null && table.Rows.Count > 0)
            {
                string dataType = "";

                if (!File.Exists("Model/"))
                {
                    Directory.CreateDirectory("Model/");
                }
                StreamWriter writer = File.CreateText("Model/" + str + ".cs");
                writer.WriteLine("using System;");
                writer.WriteLine("using System.Collections.Generic;");
                writer.WriteLine("using System.Data;");
                writer.WriteLine("using System.Data.Linq.Mapping;");
                writer.WriteLine("using System.Linq;");
                writer.WriteLine("using System.Runtime.Serialization;");
                writer.WriteLine("using System.Text;");
                writer.WriteLine("//");
                writer.WriteLine(" ");
                writer.WriteLine("namespace " + strModelNameSpace);
                writer.WriteLine("{");
                writer.WriteLine("    /// <summary>");
                writer.WriteLine("    /// ");
                writer.WriteLine("    /// </summary>");
                writer.WriteLine("    [Table(Name = \"" + str + "\")]");
                writer.WriteLine("    public class " + str + "");
                writer.WriteLine("    {");
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    DataRow dr = table.Rows[i];
                    string name = dr["字段名"].ToNullString();
                    string comment = dr["字段说明"].ToNullString();
                    string type = dr["字段类型"].ToEmptyString().Split('(')[0];
                    string key = dr["约束"].ToNullString();
                    string isNeed = dr["必填"].ToNullString();
                    string isKey = dr["主键"].ToNullString();
                    dataType = Common.GetDataType(type.ToString(), false);

                    writer.WriteLine("        /// <summary>");
                    writer.WriteLine("        /// " + comment);
                    writer.WriteLine("        /// </summary>");
                    writer.WriteLine("        public " + (isNeed.Equals("必填") ? dataType.Replace("?", "") : dataType) + " " + name + " { get; set; }");
                    writer.WriteLine(" ");
                }
                writer.WriteLine("    }");
                writer.WriteLine("}");
                writer.Flush();
                writer.Close();
            }
        }

        protected static string ConvertString(DataTable A_0, string A_1)
        {
            DataRow[] rowArray = null;
            if ((A_0 != null) && (A_0.Rows.Count > 0))
            {
                rowArray = A_0.Select(" objname = '" + A_1 + "'");
            }
            if ((rowArray != null) && (rowArray.Length > 0))
            {
                return rowArray[0]["value"].ToString().Replace("\r", " ").Replace("\n", " ");
            }
            return "";
        }
    }
}
